package com.cfg;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author <a href="15226116300@163.com">王廷俊</a>
 * @Description
 * @Date 2024-11-12
 */
public class ServiceInvocationHandler implements MethodInterceptor {
    
    protected Class<?> targetClass;
    
    public ServiceInvocationHandler() {
    }
    
    protected boolean hasProxy(String var1) {
        List var2 = null;
        
        try {
            var2 = EngineConfigurationImpl.getInstance().getDynamicProxyConfiguration().getProxyList("service", var1);
        } catch (Exception var4) {
            var4.printStackTrace();
        }
        
        return var2 != null && !var2.isEmpty();
    }
    
    public Object getProxy(Class<?> var1) {
        if (this.hasProxy(var1.getName())) {
            this.targetClass = var1;
            Enhancer var2 = new Enhancer();
            var2.setSuperclass(var1);
            var2.setCallback(this);
            return var2.create();
        } else {
            try {
                return var1.newInstance();
            } catch (InstantiationException var3) {
                throw new RuntimeException(this.targetClass.getName() + " ", var3);
            } catch (IllegalAccessException var4) {
                throw new RuntimeException(this.targetClass.getName() + " ", var4);
            }
        }
    }
    
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) {
        ServiceProxyHandler sph = new ServiceProxyHandler(targetClass, method);
        return sph.executeProxyMethod(obj, args, methodProxy);
    }
}
